package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.PriceAndInventoryModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设置库存和价格的adapter
 * Created by ivany on 2016/1/21.
 */
public class SetPriceAdapter extends BaseAdapter {

    private Context mContext;
    private List<PriceAndInventoryModel> mData;
    private List<PriceAndInventoryModel> mLastData;
    public static Map<String, String> priceData;
    public static Map<String, String> inventoryData;
    private int index = -1;
//    public interface IChangeInputListener {
//        void changeInput(int position, String price, String inventory);
//    }
//
//    private IChangeInputListener mListener;
//
//    public void setChangeInputListener(IChangeInputListener listener) {
//        mListener = listener;
//    }

    public SetPriceAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
        mLastData = new ArrayList<>();
        priceData = new HashMap<>();
        inventoryData = new HashMap<>();
    }

    public void setData(List<PriceAndInventoryModel> data) {
        mLastData.clear();
        mLastData.addAll(mData);
        mData.clear();
        mData.addAll(data);
    }

    public List<PriceAndInventoryModel> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_set_price_inventory, parent, false);
            holder = new ViewHolder();
            holder.mTxt_type = (TextView) convertView.findViewById(R.id.item_set_price_type);
            holder.mEt_price = (EditText) convertView.findViewById(R.id.item_set_price);
            holder.mEt_inventory = (EditText) convertView.findViewById(R.id.item_set_inventory);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final PriceAndInventoryModel model = mData.get(position);
        if (model.getPrice() == null || "".equals(model.getPrice())) {
            for (int i = 0; i < mLastData.size(); i++) {
                PriceAndInventoryModel lastModel = mLastData.get(i);
                if (model.getTypeLabel().equals(lastModel.getTypeLabel())) {
//                    Log.e("TAG", model.getColorLabel() + "/" + model.getSizeLabel() +
//                            " price :" + lastModel.getPrice() + ":inventory:" + lastModel.getInventory());
//                    Log.e("TAG", "last model :" + lastModel.getSizeLabel() + ":" + lastModel.getColorLabel());
                    model.setPrice(lastModel.getPrice());
                    model.setInventory(lastModel.getInventory());
                    break;
                } else {
                    model.setInventory("");
                    model.setPrice("");
                }
            }
        }
        holder.mTxt_type.setText( model.getTypeLabel());
        if (model.getPrice() != null && !"".equals(model.getPrice())) {
            holder.mEt_price.setText(model.getPrice());
        } else {
            holder.mEt_price.setText("");
        }
        if (model.getInventory() != null && !"".equals(model.getInventory())) {
            holder.mEt_inventory.setText(model.getInventory());
        } else {
            holder.mEt_inventory.setText("");
        }

        holder.mEt_price.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText editText = (EditText) v.findViewById(R.id.item_set_price);
                model.setPrice(editText.getText().toString());
//                Log.e("TAG", model.getColorLabel() + "/" + model.getSizeLabel() + " input price :" +
//                        model.getPrice());
            }
        });
        holder.mEt_inventory.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText editText = (EditText) v.findViewById(R.id.item_set_inventory);
                model.setInventory(editText.getText().toString());
//                Log.e("TAG", model.getColorLabel() + "/" + model.getSizeLabel() + " input inventory:" +
//                        model.getInventory());
            }
        });
        holder.mEt_price.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    model.setPrice(v.getText().toString());
                }
                return false;
            }
        });
        holder.mEt_inventory.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    model.setInventory(v.getText().toString());
                }
                return false;
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView mTxt_type;
        EditText mEt_price;
        EditText mEt_inventory;
    }

}
